package com.google.hackathon.game.shriko.core;

import static playn.core.PlayN.*;
import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.Pointer;

public class Title implements State {
  private final Image[] bgImage;
  private final ImageLayer[] bgLayer;
  private int currentImage;
  private float passedMilliSec = 0;
  private boolean topCliecked = false;
  private final int TOGGLE_TIME = 500;

  public Title() {
    graphics().rootLayer().clear();
    graphics().setSize(480, 800);
    bgImage = new Image[] { assetManager().getImage("images/top_bg.png"),
        assetManager().getImage("images/top_bg02.png") };

    bgLayer = new ImageLayer[] { graphics().createImageLayer(bgImage[0]),
        graphics().createImageLayer(bgImage[1]) };
    bgLayer[0].setWidth(graphics().width());
    bgLayer[1].setHeight(graphics().height());
    graphics().rootLayer().add(bgLayer[0]);
    pointer().setListener(null);
    pointer().setListener(new Pointer.Adapter() {
      public void onPointerEnd(Pointer.Event event) {
        graphics().rootLayer().clear();
        System.out.println("gae top touched");
        pointer().setListener(null);
        topCliecked = true;
      }
    });
  }

  @Override
  public void update(float delta) {
    graphics().rootLayer().clear();
    passedMilliSec += delta;
    if (passedMilliSec > TOGGLE_TIME) {
      graphics().rootLayer().clear();
      currentImage = (currentImage + 1) % bgImage.length;
      graphics().rootLayer().add(bgLayer[currentImage]);
      passedMilliSec = 0;
    }
  }

  @Override
  public void paint(float delta) {

  }

  @Override
  public boolean hasNextState() {
    return topCliecked;
  }

  @Override
  public State nextState() {
    return new Stage();
  }
}
